package com.wanmait.community.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.community.aop.OperationLogInfo;
import com.wanmait.community.entity.Room;
import com.wanmait.community.service.BuildingService;
import com.wanmait.community.service.RoomService;
import com.wanmait.community.service.UnitService;
import com.wanmait.community.util.AjaxResult;
import com.wanmait.community.util.JWTUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("admin/room")
public class RoomController {
    @Resource
    private RoomService roomService;
    @Resource
    private UnitService unitService;
    @Resource
    private BuildingService buildingService;
    @GetMapping("selectRoomByBuildingIdAndUnitId")
    public AjaxResult selectRoomByBuildingIdAndUnitId(String roomName, Integer buildingId, Integer unitId) {
        Room room = roomService.findRoomByBIdAndUIdAndRoomName(new Room().setRoomName(roomName).setBuildingId(buildingId).setUnitId(unitId));
        return AjaxResult.success().put("room", room);
    }
    @OperationLogInfo("添加房屋")
    @PostMapping("insert")
    public AjaxResult insert(@Validated @RequestBody List<Room> rooms, HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer adminId = Integer.parseInt(JWTUtils.getAudience(token));
        rooms.forEach(room -> {
            room.setCreatBy(adminId);
            room.setUpdateBy(adminId);
        });
        return AjaxResult.success(roomService.insertRoom(rooms));
    }
    @PostMapping("list")
    public AjaxResult list(@RequestBody Room room,Integer pageNum) {
        Integer pageSize = 10;
        PageInfo<Room> roomPageInfo = roomService.findPage(room,pageNum,pageSize);
        return AjaxResult.success().put("pageInfo", roomPageInfo);
    }
    @OperationLogInfo("删除一间房(不对外出售)")
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        roomService.deleteRoomById(new Room().setId(id));
        return AjaxResult.success("删除成功");
    }
    @GetMapping("{buildingId}")
    public AjaxResult getRoomByBuildingId(@PathVariable Integer buildingId) {
        List<Room> roomList = roomService.findRoomByBuildingId(buildingId);
        return AjaxResult.success().put("roomList", roomList);
    }
    @OperationLogInfo("删除楼栋")
    @GetMapping("deleteRoomByBuildingId")
    public AjaxResult deleteRoomByBuildingId(Integer buildingId) {
        roomService.deleteRoomById(new Room().setBuildingId(buildingId));
        unitService.deleteUnitByBuildingId(buildingId);
        buildingService.removeById(buildingId);
        return AjaxResult.success("删除成功");
    }
    @PostMapping("update")
    public AjaxResult update(@Validated @RequestBody Room room) {
        roomService.updateById(room);
        return AjaxResult.success();
    }
}
